package cn.com.xyc.aop.aspect;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * 切面组件，记录操作日志
 * 
 * @author Administrator
 *
 */
public class OptLogger {
	Logger logger = Logger.getLogger(OptLogger.class);

	/**
	 * 前置通知
	 */
	public void mybefore() {
		System.out.println("前置通知");
	}

	/**
	 * 后置通知
	 * 
	 * @param obj
	 */
	public void myafterReturning(Object obj) {
		System.out.println("后置通知" + obj);
	}

	/**
	 * 异常通知
	 * 
	 * @param ex
	 */
	public void myafterException(Exception ex) {
		System.out.println("异常通知 begin");
		StringBuffer b=new StringBuffer();
		StackTraceElement[] element=ex.getStackTrace();
		for(StackTraceElement ele:element){
			b.append(ele+"\n");
		}
		logger.error(b.toString());
		System.out.println("异常通知 end");
	}

	/**
	 * 最终通知
	 */
	public void myafter() {
		System.out.println("最终通知");
	}

	/**
	 * 环绕通知
	 * 
	 * @param pjp
	 * @return
	 * @throws Throwable
	 */
	public Object myaround(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("环绕通知---begin");
		// proceed()方法有执行目标对象的功能
		Object obj = pjp.proceed();

		String method = pjp.getSignature().getName();
		String clazzName = pjp.getTarget().getClass().getName();
		System.out.println("执行了" + clazzName + "的" + method + "方法");
		System.out.println("环绕通知---end");
		return obj;
	}
}
